/**
 * 模态框动态修改的jquery代码
 */
//document.write("<script language='javascript' src='indexSocket.js'></script>");

$(function(){
    $.session.set("UserClick",false);
    var font=false;
    var requestPerson=$("#meNumber").text();

    /**
     * 页面加载，获取好友列表存入session
     */
    $.post("/getMyFriends",{"ReqPerson":requestPerson},function(result){
        $.session.set("myFriends",result);
    });

    /**
     *初始化模态框
     */
    /*------------------------------------------------------------------------------*/
    $("#friends").append("<div class=\"modal-dialog modal-dialog-centered\" role=\"document\">\n" +
        "        <div class=\"modal-content\">\n" +
        "            <div class=\"modal-header\">\n" +
        "                <h4 class=\"modal-title\" id=\"myModalLabel\">我的好友</h4>\n" +
        "                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n" +
        "            </div>\n" +
        "            <div class=\"modal-body\">\n" +
        "                <p class=\"input-group\">\n" +
        "                    <input type=\"text\" class=\"form-control\" maxlength='25' placeholder=\"请输入好友名字\"/>\n" +
        "                    <span class=\"input-group-btn\">\n" +
        "                        <button class=\"btn btn-primary\">搜索</button>\n" +
        "                    </span>\n" +
        "                </p>\n" +
        "                <div id=\"userFriends\">\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class=\"modal-footer\">\n" +
        "                <button type=\"button\" id='fontFamily' class=\"btn btn-primary\">切换字体</button>\n" +
        "                <button type=\"button\" id='addFriend' class=\"btn btn-primary\">添加好友</button>\n" +
        "                <button type=\"button\" class=\"btn btn-danger\">删除好友</button>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>");
    /*------------------------------------------------------------------------------*/

    /**
     *定义jquery插件查询好友
     */
    /*--------------------------------------------------*/
    $.extend({
        "getFriends":function(){
            if($.trim($.session.get("myFriends"))!="暂未查询到好友，去添加好友") {
                var friends=JSON.parse($.session.get("myFriends"));
                $("#userFriends").html("");
                $.each(friends, function (index, value) {
                    $("#userFriends").append("<div class=\"friend tab\" alt='" + value.userNumber + "'><span class=\"friendName\">"+value.userName+"</span><span class=\"friendState\">"+value.registerState+"</span></div>");
                })
            }else{
                $("#userFriends").html("").append("<div class=\"getFriendError tab\">"+$.session.get("myFriends")+"</div>");
            }
        }
    });
    /*--------------------------------------------------*/
    $.getFriends();

    /**
     * 加载模态框（好友界面）
     */
    /*---------------------------------------------------------------*/
    $(document).on("click", '.close', function (){
        if($.session.get("UserClick")) {
            $("#myModalLabel").text("我的好友");
            $(".modal-body").html("").append("<p class=\"input-group\">\n" +
                "                    <input type=\"text\" class=\"form-control\" maxlength='25' placeholder=\"请输入好友名字\"/>\n" +
                "                    <span class=\"input-group-btn\">\n" +
                "                        <button class=\"btn btn-primary\">搜索</button>\n" +
                "                    </span>\n" +
                "                </p>\n" +
                "                <div id=\"userFriends\">\n" +
                "                </div>");
            $(".modal-footer").html("").append("<button type='button' id='fontFamily' class='btn btn-primary'>切换字体</button><button type=\"button\" id='addFriend' class=\"btn btn-primary\">添加好友</button><button type=\"button\" class=\"btn btn-danger\">删除好友</button>");
            $.session.set("UserClick",false);
            $.getFriends();
        }
    });
    /*---------------------------------------------------------------*/

    /**
     * 修改字体实现
     */
    /*--------------------------------------------------------*/
    $(document).on("click", '#fontFamily', function () {
        if(font) {
            $(this).parent().parent().parent().parent().css("font-family","none");
            font=false;
        }else{
            $(this).parent().parent().parent().parent().css("font-family","newFontFamily");
            font=true;
        }
    });
    /*--------------------------------------------------------*/

    /**
     * 加载模态框（添加好友界面）
     */
    /*----------------------------------------------*/
    $(document).on("click", '#addFriend', function (){
        $("#myModalLabel").text("好友添加");
        $(".modal-body").html("").append("<p class=\"input-group\">\n" +
            "                    <input type=\"text\" class=\"form-control\" id='addFriendNumber' maxlength='25' placeholder=\"请输入账号\"/>\n" +
            "                    <span class=\"input-group-btn\">\n" +
            "                        <button id='search' class=\"btn btn-primary\">搜索</button>\n" +
            "                    </span>\n" +
            "                </p>\n" +
            "                <div id=\"userFriends\">\n"+
            "                </div>");
        $(".modal-footer").html("");
    });

    /**
     *搜索用户功能实现
     */
    $(document).on("click", '#search', function (){
        var addFriendNumber=$("#addFriendNumber").val();
        if(addFriendNumber==""){
            alert("输入的 账号/用户名 为空");
        }else {
            $.post("/findFriendOne", {"friendNumber": addFriendNumber}, function (result) {
                if ($.trim(result) == "未找到这位同僚") {
                    $("#userFriends").html("").append("<div class='addfriend tab' style='color:red;font-size: 30px;'>"+result+"</div>");
                } else {
                    var user = JSON.parse(result);
                    $("#userFriends").html("").append("<div class='addfriend add tab' alt='" + user.userNumber + "'><span class='friendName'>" + user.userName + "</span><span class='friendState'>" + user.userNumber + "</span></div>");
                }
            });
        }
    });

    /**
     * 添加好友功能实现
     */
    $(document).on("click", '.add', function (){
        var me=$("#userNumber").text();
        var friend="账号:"+$(this).attr("alt");
        if($.trim(me)!=$.trim(friend)){
            if(confirm("确定添加好友？")){
                var reqPerson=me;
                var ResPerson=friend;
                $.post("/addFriend",{"ReqPerson":reqPerson,"ResPerson":ResPerson},function(result){
                    alert(result);
                    //window.location.reload();
                })
            }
        }
    });
    /*----------------------------------------------*/
})